package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends CheckPermissionsActivity  {

    private Button but_tl ;
    private TextView tv_text ,tv_q,tv_w,tv_e;

    private List<HashMap<String, String>> list ;

    private Handler mHandler;

    private MobileBook mobileBookSucess;
    private String mobilsJson ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        but_tl = (Button) findViewById(R.id.but_tl);
        tv_text = (TextView) findViewById(R.id.tv_text);
        tv_q = (TextView) findViewById(R.id.tv_q);
        tv_w = (TextView) findViewById(R.id.tv_w);
        tv_e = (TextView) findViewById(R.id.tv_e);
        but_tl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//获取当前时间
                Date date = new Date(System.currentTimeMillis());
//                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
//                startActivityForResult(intent, 1);
                tv_q.setText(simpleDateFormat.format(date));
                Log.d("===tt1",simpleDateFormat.format(date));
                threadInfo();

            }
        });

        handlemobilebookSucess();
    }


    /**
     * 获取手机联系人列表
     */

    private void handlemobilebookSucess () {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
// 接收消息
                if (msg.what == 1) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
                    Date date = new Date(System.currentTimeMillis());
                    tv_e.setText("日期时间hou"+simpleDateFormat.format(date));

                }else {

                }
            }
        };
    }
    private void threadInfo () {
        new Thread() {
            @Override
            public void run() {
                super.run();
                mobileBookSucess =  getAllContactInfo(MainActivity.this);
                Message msg = new Message();
                msg.what = 1;
                mHandler.sendMessage(msg);
            }
        }.start();
    }


    private String username ,usernumber;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
            Date date = new Date(System.currentTimeMillis());
            tv_q.setText("Date获取当前日期时间"+simpleDateFormat.format(date));


            Log.d("===tt",simpleDateFormat.format(date));
        }
    }


    public  MobileBook getAllContactInfo(Context context) {
//        SystemClock.sleep(3000);
//        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        List<MobileBook.MobilebookBean> listbean = new ArrayList<>() ;
        MobileBook mobileBook = new MobileBook();
        // 1.获取内容解析者
        ContentResolver resolver = context.getContentResolver();
        // 2.获取内容提供者的地址:com.android.contacts
        // raw_contacts表的地址 :raw_contacts
        // view_data表的地址 : data
        // 3.生成查询地址
        Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri date_uri = Uri.parse("content://com.android.contacts/data");
        // 4.查询操作,先查询raw_contacts,查询contact_id
        // projection : 查询的字段
        Cursor cursor = resolver.query(raw_uri, new String[] { "contact_id" },
                null, null, null);
        // 5.解析cursor
        while (cursor.moveToNext()) {
            // 6.获取查询的数据
            String contact_id = cursor.getString(0);
            // cursor.getString(cursor.getColumnIndex("contact_id"));//getColumnIndex
            // : 查询字段在cursor中索引值,一般都是用在查询字段比较多的时候
            // 判断contact_id是否为空
            if (!TextUtils.isEmpty(contact_id)) {//null   ""
                // 7.根据contact_id查询view_data表中的数据
                // selection : 查询条件
                // selectionArgs :查询条件的参数
                // sortOrder : 排序
                // 空指针: 1.null.方法 2.参数为null
                Cursor c = resolver.query(date_uri, new String[] { "data1",
                                "mimetype" }, "raw_contact_id=?",
                        new String[] { contact_id }, null);
                HashMap<String, String> map = new HashMap<String, String>();
                MobileBook.MobilebookBean mobilebookBean = new MobileBook.MobilebookBean();
                // 8.解析c
                while (c.moveToNext()) {
                    // 9.获取数据
                    String data1 = c.getString(0);
                    String mimetype = c.getString(1);
                    // 10.根据类型去判断获取的data1数据并保存
                    if (mimetype.equals("vnd.android.cursor.item/name")) {
                        // 姓名
//                        map.put("name", data1);
                        mobilebookBean.setName(data1);
                    }else if (mimetype.equals("vnd.android.cursor.item/phone_v2")) {
                        // 电话
//                        map.put("phone", data1);
                        mobilebookBean.setPhone(data1);
                    }
                }
                // 11.添加到集合中数据
//                list.add(map);
                listbean.add(mobilebookBean);
                mobileBook.setMobilebook(listbean);
                // 12.关闭cursor
                c.close();
            }
        }
        // 12.关闭cursor
        cursor.close();

        return mobileBook;
    }

}
